package io.ride;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class Word2Pdf
{
	public static void main(String[] args) throws IOException
	{
		String filepath = "D:/2020年项目部造价专项培训考卷-项目研发部-刘建青.docx";
		String outpath = "D:/test.pdf";

		InputStream source = new FileInputStream(filepath);
		OutputStream target = new FileOutputStream(outpath);
		Map<String, String> params = new HashMap<String, String>();

		PdfOptions options = PdfOptions.create();

		wordConverterToPdf(source, target, options, params);
	}

	private static void wordConverterToPdf(InputStream source, OutputStream target, PdfOptions options, Map<String, String> params) throws IOException
	{
		XWPFDocument doc = new XWPFDocument(source);
		paragraphReplace(doc.getParagraphs(), params);
		for (XWPFTable table : doc.getTables())
		{
			for (XWPFTableRow row : table.getRows())
			{
				for (XWPFTableCell cell : row.getTableCells())
				{
					paragraphReplace(cell.getParagraphs(), params);
				}
			}
		}
		PdfConverter.getInstance().convert(doc, target, options);
	}

	private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params)
	{

	}
}
